package com.waijiaojun.tpo.web.admin;

import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.CrawlerXiaoZhanTpoService;
import com.waijiaojun.tpo.service.DowloadResoursesToOssService;
import com.waijiaojun.tpo.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/admin")
public class CrawlerXiaoZhanTpoDataController {

    private static Logger logger = LoggerFactory.getLogger(CrawlerXiaoZhanTpoDataController.class);

    @Autowired
    private CrawlerXiaoZhanTpoService crawlerXiaoZhanTpoService;

    // 下载小站的文章
    @RequestMapping("/CrawlerXiaoZhanTpoData/{type}")
    @ResponseBody
    public RestResponse CrawlerXiaoZhanTpoData(@PathVariable String type) {
        try {
            if (StringUtils.isBlank(type) || (!"Tpo".equals(type) && !"Extra".equals(type) && !"Og".equals(type))) {
                throw new RestClientException("type不能为空，并且值为以下之一：Tpo、Extra、Og");
            }
            crawlerXiaoZhanTpoService.crawlerData(type);
            return RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        }
    }

    // 更新小站的文章
    @RequestMapping("/CrawlerXiaoZhanTpoData/updateArticle/{type}")
    @ResponseBody
    public RestResponse CrawlerXiaoZhanTpoDataForUpdateArticle(@PathVariable String type) {
        try {
            if (StringUtils.isBlank(type) || (!"Tpo".equals(type) && !"Extra".equals(type) && !"Og".equals(type))) {
                throw new RestClientException("type不能为空，并且值为以下之一：Tpo、Extra、Og");
            }
            crawlerXiaoZhanTpoService.updateArticle(type);
            return RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        }
    }

    // 类和场景建立关联关系
    @RequestMapping("/CrawlerXiaoZhanTpoData/linkCategoryAndScenario")
    @ResponseBody
    public RestResponse linkCategoryAndScenario() {
        try {
            crawlerXiaoZhanTpoService.linkCategoryAndScenario();
            return RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        }
    }

    @Autowired
    private DowloadResoursesToOssService dowloadResoursesToOssService;



    // 下载音频文件到自己的阿里云OSS
    @RequestMapping("download/articles/audio")
    @ResponseBody
    public String downloadArticleAudio() {
        logger.info(Constants.prefix_in );
        try {
            dowloadResoursesToOssService.downloadArticleAudio();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "OK";
    }

    // 下载封面图片到自己的阿里云OSS
    @RequestMapping("download/articles/cover")
    @ResponseBody
    public String downloadArticleCover() {
        logger.info(Constants.prefix_in );
        try {
            dowloadResoursesToOssService.downloadArticleCover();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "OK";
    }

}
